package org.example.adapter.exception;

import com.alibaba.cola.dto.Response;
import com.alibaba.cola.exception.BaseException;
import com.alibaba.cola.exception.BizException;
import com.alibaba.cola.exception.SysException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.ObjectError;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 全局异常处理
 */
@Slf4j
@RestControllerAdvice
public class CommonExceptionHandler {

    /**
     * 通用异常
     */
    @ExceptionHandler(Exception.class)
    public ResponseEntity<Response> handleException(Exception ex) {
        log.error(ex.getMessage(), ex);
        Response response = Response.buildFailure("SYSTEM_ERROR", "系统异常");
        return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR);
    }

    /**
     * hibernate-validator 参数验证异常
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseEntity<Response> handleException(MethodArgumentNotValidException ex) {
        List<String> errorMessageList = ex.getAllErrors().stream().map(ObjectError::getDefaultMessage).collect(Collectors.toList());
        String errorMessage = StringUtils.join(errorMessageList, ",");
        Response response = Response.buildFailure("METHOD_ARGUMENT_NOT_VALID", errorMessage);
        log.info("参数错误：{}", errorMessage);
        return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
    }

    /**
     * 参数缺失
     */
    @ExceptionHandler(MissingServletRequestParameterException.class)
    public ResponseEntity<Response> handleException(MissingServletRequestParameterException ex) {
        log.info("参数缺失", ex);
        Response response = Response.buildFailure("MISSING_REQUEST_PARAMETER", "Http请求参数缺失，" + ex.getMessage());
        return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
    }

    /**
     * 请求方式错误
     */
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public ResponseEntity<Response> handleException(HttpRequestMethodNotSupportedException ex) {
        log.info("Http请求方式错误", ex);
        Response response = Response.buildFailure("METHOD_NOT_ALLOWED", "Http请求方式错误，" + ex.getMessage());
        return new ResponseEntity<>(response, HttpStatus.METHOD_NOT_ALLOWED);
    }

    /**
     * COLA 业务异常
     */
    @ExceptionHandler(BizException.class)
    public ResponseEntity<Response> handleException(BizException ex) {
        log.warn(ex.getMessage(), ex);
        Response response = Response.buildFailure(ex.getErrCode(), ex.getMessage());
        return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR);
    }

    /**
     * COLA 异常
     */
    @ExceptionHandler(BaseException.class)
    public ResponseEntity<Response> handleException(SysException ex) {
        log.error(ex.getMessage(), ex);
        Response response = Response.buildFailure(ex.getErrCode(), ex.getMessage());
        return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR);
    }
}